<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 07/01/2019
 * Time: 11:06
 */

namespace App\Modules\UCenter;

use App\Services\UCenter\UCenterIndex;

class UCenter extends BaseUcenter
{

    //获取用户基本信息
    public function getUserInfo()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        return $userInfo;
    }

    /**
     * 根据id获取对方用户信息
     */
    public function getInfoById()
    {
        $data = $this->postData;
        return UCenterIndex::UCenter()->getInfoById($data);
    }

    /**
     * 修改用户信息
     * @return mixed
     */
    public function updateUserInfo()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['id'] = $res['res']['id'];
        if(isset($data['other_info']))
            $data['other_info'] = array_merge($data['other_info'], (array)$res['res']['other_info']);
        return UCenterIndex::UCenter()->updateUserInfo($data);
    }

    /**
     * 修改登录密码
     * @return mixed
     */
    public function editSelfLoginPassword()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::UCenter()->editSelfLoginPassword($data);
    }

    // 用户修改自己的支付密码
    public function editSelfPayPassword(): array
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = pick_array($this->postData, ['pay_password', 'old_pay_password', 're_pay_password', 'token']);
        return UCenterIndex::UCenter()->editSelfPayPassword($data);
    }

    // 用户提交实名认证信息
    public function authentication ()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = pick_array($this->postData, [
            'token',
            'id',
            'front_id_photo',
            'back_id_photo',
            'full_photo',
            'name'
        ]);
        return UCenterIndex::UCenter()->authentication($data);
    }

    // 企业进行实名认证
    public function corporateCertification ()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = pick_array($this->postData, [
            'token',
            'mail',
            'name',
            'number',
            'address',
            'province',
            'legal_person',
            'social_unified_coding',
            'front_id_photo',
            'back_id_photo',
            'business_license'
        ]);
        return UCenterIndex::UCenter()->corporateCertification($data);
    }

    //添加支付方式
    public function addPayment()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = pick_array($this->postData, ['token', 'name', 'type','account','bank_name','bank_name','is_default']);
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->addPayment($data);
    }

    /**
     * 获取支付方式列表
     * @return mixed
     */
    public function getPaymentList()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->getPaymentList($data);
    }

    public function delPaymentById()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->delPaymentById($data);
    }

    /**
     * 头像上传
     * @return array
     */
    public function upload()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->upload($data);
    }

    /**
     * 根据手机号获取短信验证码
     */
    public function getVerifyByPhone()
    {
//        $res = $this->isLogin();
//        if ($res['status'] !== SUCCESS_STATUS) {
//            return need_login($res,'请登录');
//        }
        $data = $this->postData;
//        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->getVerifyByPhone($data);
    }

    /**
     * 验证短信验证码
     * @return array
     */
    public function verifySms()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->verifySms($data);
    }

    /**
     * 验证新手机号及短信验证码并执行换绑
     * @return array
     */
    public function changePhone()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->changePhone($data);
    }

    /**
     * 签到
     */
    public function sign()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->sign($data);
    }

    /**
     * 检测用户是否签到
     * @return array
     */
    public function isSign()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->isSign($data);
    }

    /**
     * 根据手机号查找用户
     * @return array
     */
    public function getUserByPhone()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::UCenter()->getUserByPhone($data);
    }

    /**
     * 验证手机号及短信验证码并重置密码（忘记密码）
     */
    public function changeLoginPwd()
    {
        $data = $this->postData;
        return UCenterIndex::UCenter()->changeLoginPwd($data);
    }

    /**
     * 验证短信验证码并重置支付密码
     */
    public function resetPayPassword()
    {
        $res = $this->isLogin();
        if ($res['status'] !== SUCCESS_STATUS) {
            return need_login($res,'请登录');
        }
        $data = $this->postData;
        $data['id'] = $res['res']['id'];
        return UCenterIndex::UCenter()->resetPayPassword($data);
    }

}
